﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eugenhome.Atrium.Site;
using System.IO;

namespace Eugenhome.Atrium.Bookmark.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DefaultEdit : System.Web.UI.Page
    {
        /// <summary></summary>
        private ImageItemCollection _iic;

        /// <summary></summary>
        public ImageItem SelectedItem { get; set; }

        /// <summary></summary>
        public string SelectedImage { get; set; }

        /// <summary></summary>
        private List<ImageItem> _selectedList { get { return rbDay.Checked ? _iic.Items : _iic.DefaultItems; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _iic = new ImageItemCollection();

            _iic.DataFullFileName = Server.MapPath("dailyimages.xml");
            _iic.Load();
            SelectedItem = _selectedList.Where(item => item.ID.ToString().Equals(lstItems.SelectedValue)).FirstOrDefault();

            btnAdd.Enabled = rbDay.Checked;
            btnDelete.Enabled = rbDay.Checked;

            if (!IsPostBack)
            {
                UpdateBookmarkList();

                if (!lstItems.Items.Count.Equals(0))
                {
                    lstItems.SelectedIndex = 0;

                    lstBookmarks_SelectedIndexChanged(null, null);
                }

                //Server-Bilder aktualisieren und anzeigen
                UpdateServerImages();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateServerImages()
        {
            lstDailyImages.DataSource = new DailyImageFile("").GetFiles(Server.MapPath(""));
            lstDailyImages.DataTextField = "DisplayName";
            lstDailyImages.DataValueField = "FullFileName";
            lstDailyImages.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lstBookmarks_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedItem = _selectedList.Where(item => item.ID.ToString().Equals(lstItems.SelectedValue)).FirstOrDefault();
            this.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lstDailyImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedImage = new DailyImageFile(lstDailyImages.SelectedValue).HttpPath;
            this.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateBookmarkList()
        {
            lstItems.DataSource = _selectedList.Select(item => new DailyImageItem(item)).ToList();
            lstItems.DataTextField = "DisplayName";
            lstItems.DataValueField = "ID";
            lstItems.DataBind();

            if (SelectedItem != null)
            {
                try
                {
                    lstItems.SelectedValue = SelectedItem.ID.ToString();
                }
                catch { }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            SelectedItem.Day = txtDay.Text;
            SelectedItem.Desc = txtDesc.Text;
            SelectedItem.DescHref = txtDescHref.Text;
            SelectedItem.ImageHref = txtImageHref.Text;

            SelectedItem.Update();

            UpdateBookmarkList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            SelectedItem = _selectedList.Where(item => item.ID.ToString().Equals(lstItems.SelectedValue)).FirstOrDefault();

            if (SelectedItem != null)
            {
                SelectedItem.Delete();

                if (!lstItems.Items.Count.Equals(0))
                {
                    UpdateBookmarkList();

                    lstItems.SelectedIndex = 0;

                    lstBookmarks_SelectedIndexChanged(null, null);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            SelectedItem = new ImageItem();
            SelectedItem.Day = "";
            SelectedItem.Desc = "";
            SelectedItem.DescHref = "";
            SelectedItem.ImageHref = "";

            _iic.Add(SelectedItem);

            SelectedItem.Update();

            this.DataBind();

            UpdateBookmarkList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCopy_Click(object sender, ImageClickEventArgs e)
        {
            SelectedItem = new ImageItem();
            SelectedItem.Day = txtDay.Text;
            SelectedItem.Desc = txtDesc.Text;
            SelectedItem.DescHref = txtDescHref.Text;
            SelectedItem.ImageHref = txtImageHref.Text;

            _iic.Add(SelectedItem);

            SelectedItem.Update();

            this.DataBind();

            UpdateBookmarkList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rbDay_CheckedChanged(object sender, EventArgs e)
        {
            UpdateBookmarkList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFileUpload_Click(object sender, EventArgs e)
        {
            if (fileUpload.HasFile)
                try
                {
                    string targetpath = Server.MapPath(Path.Combine(DailyImageFile.IMAGEFOLDER, fileUpload.FileName));
                    fileUpload.SaveAs(targetpath);
                }
                catch (Exception ex)
                {
                    //Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                // Label1.Text = "You have not specified a file.";
            }

            //Server-Bilder aktualisieren und anzeigen
            UpdateServerImages();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnImageDelete_Click(object sender, ImageClickEventArgs e)
        {
            if (lstDailyImages.SelectedValue != null && File.Exists(lstDailyImages.SelectedValue))
            {
                try
                {
                    File.Delete(lstDailyImages.SelectedValue);
                }
                catch { }

                //Server-Bilder aktualisieren und anzeigen
                UpdateServerImages();
            }

        }



    }
}